package jdbc;

import dao.UserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import pojo.User;

public class TestUser {
    @Test
    public void findAllUserTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        System.out.println(userDao.findAllUser());
    }
    @Test
    public void findUserByIdTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        System.out.println(userDao.findUser(1));
    }
    @Test
    public void addUserTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        User user = new User();
        user.setUsername("jfldjlfajlfja");
        user.setPassword("jdljflajf");
        user.setSex("male");
        System.out.println(userDao.addUser(user));
    }
    @Test
    public void deleteUserTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        System.out.println(userDao.deleteUser(3));
    }
    @Test
    public void updateUserTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        User user = new User();
        user.setId(1);
        user.setUsername("jdljl");
        user.setSex("midldesex");
        user.setPassword("jdlflajflaj");
        System.out.println(userDao.updateUser(user));
    }
    @Test
    public void countTest(){
        ClassPathResource classPathResource = new ClassPathResource("applicationContext.xml");
        ApplicationContext context  = new ClassPathXmlApplicationContext(classPathResource.getPath());
        UserDao userDao = (UserDao) context.getBean("userDao");
        System.out.println(userDao.getByUserName("test"));
    }
}
